/*
	File: data/scripts/cannon.nut
	Author: Astrofra
*/

	Include("data/scripts/globals.nut")

//---------------
class	Explosion
//---------------
{
	scene					=	0
	item_explosion			=	0
	
	constructor(_scene, _position, _size = 5.0, _delay = 0.0)
	{
		print("Explosion:constructor()")
		scene = _scene
		_size = _size.tostring()
		_delay = _delay.tostring()
		local explosion_geo
		explosion_geo = EngineLoadGeometry(g_engine, "data/meshes/explosion_round.nmg")
		item_explosion = SceneAddObject(scene, "explosion")
		ObjectSetGeometry(item_explosion, explosion_geo)
		item_explosion = ObjectGetItem(item_explosion)
		ItemSetPosition(item_explosion, _position)
		ItemSetScale(item_explosion, Vector(0,0,0))
		ItemSetup(item_explosion)
		ItemSetCommandList(item_explosion, "nop " + _delay + ";toscale 0.0,2,2,2;toscale 0.025," + _size + "," + _size + "," + _size + ";nop 0.025;toscale 0.075,2,2,2;toscale 0.25,0,0,0;")
		ItemSetScript(item_explosion, "data/scripts/cannon.nut", "ExplosionRuntime")
		ItemSetupScript(item_explosion)
	}
}

//----------------------
class	ExplosionRuntime
//----------------------
{
	light_explosion			=	0
	scene					=	0
	intensity				=	0.0

	function	OnSetup(item)
	{
		scene = ItemGetScene(item)
		light_explosion = SceneAddLight(scene, "bullet_light")
		LightSetDiffuseColor(light_explosion, Vector(1.0, 0.65, 0.15))
		LightSetSpecularColor(light_explosion, Vector(0.0, 0.0, 0.0))
		LightSetDiffuseIntensity(light_explosion, 0.0)
		LightSetType(light_explosion, LightTypePoint)
		LightSetRange(light_explosion, Mtr(2.0))
		LightSetVolumeRange(light_explosion, Mtr(2.0))
		ItemSetParent(LightGetItem(light_explosion), item)
		intensity	=	0.0
	}

	function	OnUpdate(item)
	{

		intensity += g_dt_frame
		intensity = Clamp(intensity, 0.0, 1.0)

		LightSetDiffuseIntensity(light_explosion, MakeTriangleWave(intensity))

		if (ItemIsCommandListDone(item))
		{
			local	scene
			scene = ItemGetScene(item)
			SceneDeleteItem(scene, item)
			SceneDeleteItem(scene, LightGetItem(light_explosion))
		}
	}
}

//------------
class	Bullet
//------------
{

	bullet_light	=	0
	scene			=	0
	direction		=	0
	position		=	0
	previous_pos	=	0
	col_mask		=	0

	function	OnSetup(item)
	{
		scene = ItemGetScene(item)
		position = ItemGetPosition(item)
		previous_pos = clone(position)
		//bullet_light = SceneFindItemChild(scene, item, "bullet_light")
	}

	function	OnUpdate(item)
	{
		if (ItemIsCommandListDone(item))
			SceneDeleteItem(scene, item)

		position = ItemGetPosition(item)

		DrawDebugPoint(position)

		local	vel = (position - previous_pos).Len()
		local	hit = SceneCollisionRaytrace(scene, position, direction, 2, CollisionTraceAll, vel)

		if (hit.hit)
			SceneDeleteItem(scene, item)

		previous_pos = clone(position)
	}
}

//------------
class	Cannon
//------------
{
	cannon_item		=	0
	bullet_geo		=	0
	scene			=	0
	is_player		=	false
	bullet_name		=	"bullet"
	bullet_col_mask	=	0 //with_mask.bullet_enemy

	constructor(item)
	{
		print("Cannon:constructor()")
		cannon_item = item
		scene = ItemGetScene(item)
		bullet_geo = EngineLoadGeometry(g_engine, "data/meshes/bullet_round.nmg")
		bullet_col_mask	= with_mask.bullet_enemy
	}

	function	Fire(offset = 0.0)
	{
		print("Cannon::Fire()")
		local	_new_obj, _new_bullet, _new_light

		_new_obj = SceneAddObject(scene, bullet_name)	//is_player?"player_bullet":"bullet")
		_new_bullet = ObjectGetItem(_new_obj)
		ItemRenderSetup(_new_bullet, g_factory)
		ObjectSetGeometry(_new_obj, bullet_geo)

		local	_dir, _pos, _dest
		_dir = ItemGetMatrix(cannon_item).GetFront()
		_pos = ItemGetWorldPosition(cannon_item) + _dir.Scale(0.25)
		ItemSetPosition(_new_bullet, _pos)
		_dest = _pos + _dir.Scale(Mtr(50.0))

		DrawDebugLine(_pos, _pos + _dir.Scale(2.0))

		ItemSetScript(_new_bullet, "data/scripts/cannon.nut", "Bullet")
		ItemSetupScript(_new_bullet)
		ItemSetup(_new_bullet)
		ItemGetScriptInstance(_new_bullet).direction = _dir
		ItemGetScriptInstance(_new_bullet).col_mask = bullet_col_mask
		
		ItemSetCommandList(_new_bullet, "toposition 5," + _dest.x.tostring() + "," + _dest.y.tostring() + "," + _dest.z.tostring() + ";")
/*	
		ItemSetPosition(_new_bullet, Vector(0,-10000.0,0))	// GRUIIIIIK
		ItemSetPhysicMode(_new_bullet, PhysicModeDynamic)
		ItemPhysicSetLinearFactor(_new_bullet, Vector(1.0,0.0,1.0))
		local	_shape = ItemAddCollisionShape(_new_bullet)
		ShapeSetBox(_shape, Vector(0.25,0.25,0.25))
		ShapeSetMass(_shape, 1.0)

		_new_light = SceneAddLight(scene, "bullet_light")
		LightSetDiffuseColor(_new_light, Vector(1.0, 0.65, 0.15))
		LightSetSpecularColor(_new_light, Vector(1.0, 0.65, 0.15))
		LightSetDiffuseIntensity(_new_light, 1.5)
		LightSetType(_new_light, LightTypePoint)
		LightSetRange(_new_light, Mtr(2.0))
		LightSetVolumeRange(_new_light, Mtr(2.0))
		ItemSetParent(LightGetItem(_new_light), _new_bullet)

		ItemSetup(_new_bullet)
		ItemSetScript(_new_bullet, "data/scripts/cannon.nut", "Bullet")
		ItemSetupScript(_new_bullet)

		ItemSetPhysicPosition(_new_bullet, ItemGetWorldPosition(cannon_item) + ItemGetMatrix(cannon_item).GetLeft().Scale(offset))
		ItemApplyLinearImpulse(_new_bullet, ItemGetMatrix(cannon_item).GetFront().Scale(25.0))

		ItemSetCollisionMask(_new_bullet, bullet_col_mask)
		ItemSetSelfMask(_new_bullet, bullet_col_mask)
*/
		
	}
}

//---------------------------------
class	PlayerCannon extends Cannon
//---------------------------------
{
	
	constructor(item)
	{
		base.constructor(item)
		bullet_name = "player_bullet"
		bullet_col_mask	= with_mask.bullet_player
		print("PlayerCannon::constructor() bullet_col_mask = " + bullet_col_mask)
	}
	
	function	Fire(offset = 0.0)
	{
		base.Fire(offset)
	}
}
